package haoyou.csnf.manager.module.pm.service.rulesbasepay;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.rulesbasepay.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.rulesbasepay.RulesBasepayDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.rulesbasepay.RulesBasepayMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 基本薪资规则 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class RulesBasepayServiceImpl implements RulesBasepayService {

    @Resource
    private RulesBasepayMapper rulesBasepayMapper;

    @Override
    public Integer createRulesBasepay(RulesBasepaySaveReqVO createReqVO) {
        // 插入
        RulesBasepayDO rulesBasepay = BeanUtils.toBean(createReqVO, RulesBasepayDO.class);
        rulesBasepayMapper.insert(rulesBasepay);
        // 返回
        return rulesBasepay.getId();
    }

    @Override
    public void updateRulesBasepay(RulesBasepaySaveReqVO updateReqVO) {
        // 校验存在
        validateRulesBasepayExists(updateReqVO.getId());
        // 更新
        RulesBasepayDO updateObj = BeanUtils.toBean(updateReqVO, RulesBasepayDO.class);
        rulesBasepayMapper.updateById(updateObj);
    }

    @Override
    public void deleteRulesBasepay(Integer id) {
        // 校验存在
        validateRulesBasepayExists(id);
        // 删除
        rulesBasepayMapper.deleteById(id);
    }

    @Override
        public void deleteRulesBasepayListByIds(List<Integer> ids) {
        // 校验存在
        validateRulesBasepayExists(ids);
        // 删除
        rulesBasepayMapper.deleteByIds(ids);
        }

    private void validateRulesBasepayExists(List<Integer> ids) {
        List<RulesBasepayDO> list = rulesBasepayMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RULES_BASEPAY_NOT_EXISTS);
        }
    }

    private void validateRulesBasepayExists(Integer id) {
        if (rulesBasepayMapper.selectById(id) == null) {
            throw exception(RULES_BASEPAY_NOT_EXISTS);
        }
    }

    @Override
    public RulesBasepayDO getRulesBasepay(Integer id) {
        return rulesBasepayMapper.selectById(id);
    }

    @Override
    public PageResult<RulesBasepayDO> getRulesBasepayPage(RulesBasepayPageReqVO pageReqVO) {
        return rulesBasepayMapper.selectPage(pageReqVO);
    }

}